using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.OleDb;

namespace LogAnalyzerIIS.Exporters
{
	partial class ExportToSQLWizard
	{
		// Fields on the data provider page have changed
		private bool providerChangedOLEDB = true;

		// List of the OLE DB data providers
		private List<object[]> OleDbProviders;

		/// <summary>
		/// Prepares the OLE DB data provider page
		/// </summary>
		/// <returns>Success</returns>
		private bool PrepareOleDb()
		{
			// if there are some providers in combobox already then do nothing
			if (cbOleDbProviders.Items.Count > 0)
				return true;

			// Get root enumerator for enumerating OLE DB data providers
			DbDataReader dataReader = OleDbEnumerator.GetRootEnumerator();

			// If enumerator hasn't been set then show error
			if (dataReader == null)
			{
				MessageBox.Show("Cannot get OLE DB provider list.", "OLE DB data provider", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			// Clear combobox with OLE DB data provider list
			cbOleDbProviders.Items.Clear();

			// Create new list of OLE DB data providers
			OleDbProviders = new List<object[]>();

			// Move through data providers
			while (dataReader.Read())
			{
				// Create array of objects for storing information about data providers
				object[] dataProviderObjects = new object[dataReader.FieldCount];

				try
				{	// Try to get information
					dataReader.GetValues(dataProviderObjects);
				}
				catch (Exception ex)
				{
					// Show error message
					MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK,
					MessageBoxIcon.Error);
					return false;
				}

				//TODO: [+] Filter on adding OLE DB Provider
				if ((dataProviderObjects[3].ToString() == "1") &&
										(!dataProviderObjects[2].ToString().Contains("Service")) &&
					(dataProviderObjects[0].ToString() != "MSDASQL"))
				{
					// Add OLE DB data provider to the list of OLE DB data providers
					OleDbProviders.Add(dataProviderObjects);

					// Add OLE DB data provider to the combobox
					cbOleDbProviders.Items.Add(dataProviderObjects[2].ToString());
				}
			}
			// return success
			return true;
		}

		/// <summary>
		/// Makes connection string to connect to OLE DB data provider from data of wizard's fields
		/// </summary>
		/// <returns>Connection string</returns>
		private string GetOleDbConnectionString()
		{
			string connectionString = "";

			// If provider is selected then get its name
			if (cbOleDbProviders.SelectedIndex >= 0)
				connectionString = "Provider=" + OleDbProviders[cbOleDbProviders.SelectedIndex][0].ToString();

			// Set data source (server)
			if (tbOleDbDataSource.Text != "")
				connectionString += ";Data Source=" + tbOleDbDataSource.Text;

			// Initial catalog (database)
			if (tbOleDbInitialCatalog.Text != "")
				connectionString += ";Initial catalog=" + tbOleDbInitialCatalog.Text;

			// Authentication (windows auth)
			if (rbOleDbWindowsAuth.Checked)
				connectionString += ";Integrated Security=true";
			else
			{
				// Use auth info from fields
				if (tbOleDbUserId.Text != "")
					connectionString += ";User ID=" + tbOleDbUserId.Text;

				if (tbOleDbPassword.Text != "")
					connectionString += ";Password=" + tbOleDbPassword.Text;
			}

			return connectionString;
		}

		/// <summary>
		/// Validates fields on the "OLE DB data provider" page and enable Next button
		/// </summary>
		private void TryOleDbNextButtonEnable()
		{
			if (cbOleDbProviders.Text != "" &&
					(tbOleDbInitialCatalog.Text != "" ||
		(tbOleDbDataSource.Text != "")))
				btnNext.Enabled = true;
			else
				btnNext.Enabled = false;
		}

		/// <summary>
		/// Event which occurs when any field on OLE DB data provider page is changed
		/// </summary>
		private void OleDbParametersChanged(object sender, EventArgs e)
		{
			TryOleDbNextButtonEnable();		// Try to enable Next button
			providerChangedOLEDB = true;	// Set "changed" flag
		}

		/// <summary>
		/// Event which occurs then checked state of "UserAuth" radiobutton on OLE DB page is changed
		/// </summary>
		private void rbOleDbUserAuth_CheckedChanged(object sender, EventArgs e)
		{
			bool b = (sender as RadioButton).Checked;

			tbOleDbUserId.Enabled = b;
			tbOleDbPassword.Enabled = b;
			lblOleDbUserId.Enabled = b;
			lblOleDbPassword.Enabled = b;

			providerChangedOLEDB = true;
		}

		/// <summary>
		/// Event which occurs then user nameon OLE DB page is changed
		/// </summary>
		private void OleDbAuthParameterChanged(object sender, EventArgs e)
		{
			providerChangedOLEDB = true;
		}

		/// <summary>
		/// Selecting an OLE DB database with OpenDialog
		/// </summary>
		private void btnBrowseOleDb_Click(object sender, EventArgs e)
		{
			OpenDataBaseFileDialog1.Title = "Select database file...";

			OpenDataBaseFileDialog1.Filter = "All files|*.*";
			OpenDataBaseFileDialog1.DefaultExt = "";

			if (OpenDataBaseFileDialog1.ShowDialog() == DialogResult.OK)
			{
				tbOleDbDataSource.Text = OpenDataBaseFileDialog1.FileName;
				TryOleDbNextButtonEnable();
			}
		}
	}
}